﻿using AutoMapper;
using Paperless.Entities;
using Paperless.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Paperless
{
    public class MappingConfig
    {
        public static void RegisterMappings()
        {
            Mapper.CreateMap<Status, StatusModel>().ReverseMap();
            Mapper.CreateMap<Person, PersonModel>().ReverseMap();
            Mapper.CreateMap<Contact, ContactModel>().ReverseMap();
            Mapper.CreateMap<Location, LocationModel>().ReverseMap();

            Mapper.CreateMap<NoteModel, Note>();
            Mapper.CreateMap<Note, NoteViewModel>();

            Mapper.CreateMap<User, UserModel>().ReverseMap();

            Mapper.CreateMap<Agent, AgentModel>()
                .ForMember(target => target.Name, opt => opt.MapFrom(source => source.User.UserName))
                .ForMember(target => target.Email, opt => opt.MapFrom(source => source.User.Email))
                .ReverseMap();

            Mapper.CreateMap<Agent, AgentContactModel>()
                .ForMember(target => target.Name, opt => opt.MapFrom(source => source.User.UserName))
                .ForMember(target => target.Email, opt => opt.MapFrom(source => source.User.Email));

            Mapper.CreateMap<Transaction, TransactionModel>()
                .ForMember(target => target.StatusName, opt => opt.MapFrom(source => source.Status.Name))
                .ForMember(target => target.LocationName, opt => opt.MapFrom(source => source.Location.Name))
                .ForMember(target => target.Notes, opt => opt.Ignore())
                .ForMember(target => target.Contacts, opt => opt.Ignore())
                .ReverseMap();

            Mapper.CreateMap<User, UserSearchModel>()
                .ForMember(target => target.Name, opt => opt.MapFrom(source => source.FirstName + " " + source.LastName));
        }
    }
}